//
//  ShoppingCarBottomView.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/17.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class ShoppingCarBottomView: UIView {
    
    weak var delegate : ShoppingCarCellDelegate?{
        
        didSet{
            
            if delegate == nil {
                return
            }
            
            self.VC = delegate as? ShoppingCarController
            
        }
        
    }
    
    weak var VC : ShoppingCarController?


    @IBOutlet weak var leftSelectBtn: UIButton!
    
    @IBOutlet weak var sumLabel: UILabel!
    
    
    @IBOutlet weak var payBtn: UIButton!
    
    
    override func awakeFromNib() {
        
        super.awakeFromNib()
        
        leftSelectBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
        
        leftSelectBtn.isSelected = true
        
        
    }

    
    
    @IBAction func leftSelectBtnDidTap(_ sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
        
        for item in VC!.shopCartList {
            item.isSelect = sender.isSelected
            for subItem in item.goods {
                subItem.isSelect = sender.isSelected
            }
        }
        
        VC?.calculateTotalPrice()
        
        VC?.plainTableView.reloadData()
        
    }
    
    @IBAction func payBtnDidTap(_ sender: UIButton) {
        
        if VC?.getCarInfoStr() != nil {
            
            VC?.isTapDisappear = true
            
            let temp =  ShopCarOrderVC()
            
            temp.navigationItem.title = "订单确认"
            
            temp.shopInfoStr = VC?.getCarInfoStr()
            
            viewController().navigationController?.pushViewController(temp, animated: true)
            
 
        } else {
            
            let alerV = UIAlertController(title: nil, message: "请勾选要下单的商品!", preferredStyle: .alert)
            
            let ation1 = UIAlertAction(title: "取消", style: .default, handler: { (_) in
                
            })
            
            let ation2 = UIAlertAction(title: "确定", style: .destructive, handler: { (_) in
                
                
            })
            
            alerV.addAction(ation1)
            alerV.addAction(ation2)
            
            VC?.present(alerV, animated: true, completion: nil)
            
        }
        
    }
}
